layui.config({
    base : "/dhjdglxt/modules/util/"//自定义加载的模块
}).extend({
    formSelects: 'formSelects-v4.min'
}).use(['form','layer','jquery','layedit','laydate','laypage','table','commUtil','element','treetable',"formSelects"],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : parent.layer,
        laypage = layui.laypage,
        layedit = layui.layedit,
        laydate = layui.laydate,
        $ = layui.jquery;
    var table = layui.table;
    var commUtil = layui.commUtil;
    var element = layui.element;
    var treetable = layui.treetable;//很重要
    var formSelects = layui.formSelects;
    
    var airFields = [
    	{'gljh':'关联计划'},
    	{'zch':'注册号'},
    	{'ccxlh':'出厂序列号'},
    	{'fsn':'东航机队号/fsn'},
    	{'airZt':'飞机状态'},
    	{'nature':'飞机性质'},
    	{'pw':'批文'},
    	{'startTime':'租赁起始日期'},
    	{'endTime':'租赁到期日期'},
    	{'sign':'国籍标志和登记标志'},
    	{'airModel':'航空器型号'},
    	{'airZzs':'航空器制造厂商'},
    	{'outSn':'航空器出厂序号'},
    	{'proctionDate':'出厂日期'},
    	{'useDate':'调机日期'},
    	{'offDate':'注销日期'},
    	{'engineS':'engineSerie'},
    	{'engine':'engine'},
    	{'oneSn':'出厂序号1'},
    	{'twoSn':'出厂序号2'},
    	{'gjNo':'国籍登记证编号'},
    	{'gjDate':'国籍登记证签发日期'},
    	{'bzfj':'本证发给'},
    	{'htMoney':'合同金额'},
    	{'haveName':'所有人名称'},
    	{'haveAddr':'所有人地址'},
    	{'yyName':'运营人'},
    	{'yyAddr':'运营人地址'},
    	{'shNo':'标准适航证编号'},
    	{'qfDate':'标准适航证签发日期'},
    	{'hjNo':'选择呼叫代码'},
    	{'addrNoHex':'航空器地址编码hex'},
    	{'addrNoOct':'航空器地址编码oct'},
    	{'dtNo':'电台执照序号'},
    	{'dtyxDate':'电台执照有效期限至'},
    	{'dtbfDate':'电台执照颁发日期'},
    	{'dtz':'电台证'},
    	{'shz':'适航证'},
    	{'gjz':'国籍证'},
    	{'ht':'合同电子档'},
    	{'htsqxx':'合同授权信息'},
    	{'fjyz':'飞机原值'},
    	{'fjcz':'飞机残值'},
    	{'bxxx':'保险信息'},
    	{'zjbz':'租金币种'},
    	{'mqzj':'每期租金'},
    	{'zjzfqj':'租金支付期间'},
    	{'hl':'汇率'},
    	{'jrygrq':'加入运归日期'},
    	{'tcygrq':'退出运规日期'},
    	{'apu':'apu型号'},
    	{'hud':'hud'},
    	{'wifi':'wifi'},
    	{'maxZl':'最大起飞重量'},
    	{'zyqdj':'占有权登记'},
    	{'idera':'idera登记'},
    	{'zwbj':'座位布局'}
    ]
    
    for (var i = 0; i < airFields.length; i++) {
    	for (var key in airFields[i]) {
    		$("#s_name").append('<option value=\"'+key+'\">'+airFields[i][key]+'</option>')
    	}
    	
    	formSelects.render('s_name');
	}
    
    var yshead = [
    	{type:'numbers',title: '序号',align:'center'}
    ];
    
    var colList = [];
    formSelects.on('s_name', function(id, vals, val, isAdd, isDisabled){
    	colList = vals;
        //id:           点击select的id
        //vals:         当前select已选中的值
        //val:          当前select点击的值
        //isAdd:        当前操作选中or取消
        //isDisabled:   当前选项是否是disabled
         
    }, true);
    
    
    $("#reset").on("click",function () {
    	colList = [];
    });
    
    
    $(".onQuery").click(function(){
        onQuery();
    })
    
    function onQuery(){
    	var headerAppend = [];
    	var header = []
    	if(colList.length > 0){
    		for (var i = 0; i < colList.length; i++) {
    			if(colList[i].value == 'gljh'){
    				headerAppend.push(
	    				{
	    	    			field:colList[i].value, 
	    	    			title: colList[i].name,
	    	    			align:'center',
	    	    			minWidth: 150,
	    	    			templet:'#gljh'
	    				}
	    			)
    			} else if(colList[i].value == 'dtz' || colList[i].value == 'shz' || colList[i].value == 'gjz' || colList[i].value == 'ht' || colList[i].value == 'pw') {
    				headerAppend.push(
	    				{
	    	    			field:colList[i].value, 
	    	    			title: colList[i].name,
	    	    			align:'center',
	    	    			minWidth: 150,
	    	    			templet:function(d) {
	    	    				return getFileName(d[this.field]);
	    	    			}
	    				}
	    			)
    			} else {
    				headerAppend.push(
	    				{
	    	    			field:colList[i].value, 
	    	    			title: colList[i].name,
	    	    			align:'center',
	    	    			minWidth: 150
	    				}
	    			)
    			}
        		
    		}
    		header = yshead.concat(headerAppend);
    	}
		table.render({
	        elem: '#myTable'
	        ,id:'myTable'
	        ,cols: [header]
			,where:{
				limit: Number.MAX_VALUE
			}
	    	,page: false
	        ,url: basePath + '/service/cxtj/airList'
	        , done: function (res,curr,count) {
	            this.cols = []
	        }
	    });
    }
    
    $(".ondc").click(function(){
    	var url = basePath + '/service/cxtj/airList/down';
    	var urlfj = basePath + '/service/cxtj/airList/down/fj';
    	var params = {'s_list':JSON.stringify(colList)};
    	for(var i = 0 ; i<colList.length; i++ ){
    		if(colList[i].value == 'dtz' || colList[i].value == 'shz' || colList[i].value == 'gjz' || colList[i].value == 'ht' || colList[i].value == 'pw'){
    			layer.msg('选中列中包含附件，是否将附件一同下载？', {
				  time: 0 //不自动关闭
				  ,btn: ['是', '否']
				  ,yes: function(index){
					  layer.close(index);
					  console.log("开始下载附件-----------------------");
					  postExcelFile(params,urlfj);
				  }
				});
    		}
    	}
    	console.log("开始下载excel-----------------------")
    	postExcelFile(params,url)
    })
    
    function postExcelFile(params, url) {
      var form = document.createElement("form");
      form.style.display = "none";
      form.action = url;
      form.method = "post";
      document.body.appendChild(form);
      for (var key in params) {
        var input = document.createElement("input");
        input.type = "hidden";
        input.name = key;
        input.value = params[key];
        form.appendChild(input);
      }
      form.submit();
      form.remove();
    }
    
    
    function getFileName(path) {
    	var show = '';
    	try {
    		var fileIds = path.split(",");
            for(var i=0;i<fileIds.length;i++) {
                var fjid = fileIds[i];
                $.ajax({
                    type: "GET",
                    url: UploaderPath + 'service/files',
                    data: {s_files: path},
                    async: false,
                    dataType: "json",
                    success: function (data) {
                    	console.log(data);
                    	var fileName = data.data.objList[0].fileName;
                    	if(i>0){ 
                    		show += '<br><a  href="'+window.UploaderPath+'service/download/pfile/'+path+ '" target=_blank  style="color: deepskyblue" >' + fileName+' </a>'
                    	} else {
                    		show += '<a  href="'+window.UploaderPath+'service/download/pfile/'+path+ '" target=_blank  style="color: deepskyblue" >' + fileName+ ' </a>'
                    	}
                    }
            	})
            }
    	} catch(e){
    		
    	}
        return show;
    }
})

function HTMLDecode(text) {
    var temp = document.createElement("div");
    temp.innerHTML = text;
    var output = temp.innerText || temp.textContent;
    temp = null;
    return output;
} 	